package com.abcgz.grade.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abcgz.grade.GradeException;
import com.abcgz.grade.dao.GradeRecord;
import com.abcgz.grade.dao.GradeRecordDao;
import com.abcgz.grade.dao.Innovate;
import com.abcgz.grade.dao.InnovateDao;
import com.abcgz.grade.dao.User;
import com.abcgz.grade.model.GradeVO;
import com.abcgz.grade.model.InnovateVO;
import com.abcgz.grade.model.RestResult;
import com.abcgz.grade.service.GradeService;

@Controller
public class GradeController {
	@Autowired
	private InnovateDao innovateDao;
	
	@Autowired
	private GradeRecordDao gradeRecordDao;
	
	@Autowired
	private GradeService gradeService;
	
	@RequestMapping(value = "/grade", method = RequestMethod.GET)
    public String index() {
        return "grade";
    }
	
	@RequestMapping(value = "/result", method = RequestMethod.GET)
    public String result() {
        return "result";
    }
	
	@RequestMapping(value = "/gradeResult", method = RequestMethod.GET)
    public String gradeResult() {
        return "gradeResult";
    }
	
	@RequestMapping(value = "/innovate", method = RequestMethod.GET)
    public String innovate() {
        return "innovate";
    }
	
	@RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        return "info";
    }
	
	
	@RequestMapping(value = "/getInnovates", method = RequestMethod.GET)
	@ResponseBody
	public RestResult getInnovates(){
		User user = (User)SecurityUtils.getSubject().getPrincipal();
		List<Innovate> list = this.innovateDao.findAll();
		list.sort((i1, i2) -> i1.getDisplayOrder().compareTo(i2.getDisplayOrder()));
		List<InnovateVO> result = new ArrayList<InnovateVO>();
		for(Innovate i: list) {
			InnovateVO vo = new InnovateVO();
			vo.setDescription(i.getDescription());
			vo.setId(i.getId());
			vo.setMaxScore(i.getMaxScore());
			vo.setMinScore(i.getMinScore());
			vo.setScore(i.getScore());
			vo.setStatu(i.getStatu());
			vo.setTitle(i.getTitle());
			GradeRecord rec = this.gradeRecordDao.findByInnovateIdAndUsername(i.getId(), user.getUsername());
			if(rec != null) {
				vo.setStatu(2);
			}
			result.add(vo);
		}
		return new RestResult(result);
	}
	
	@RequestMapping(value = "/getInnovateItem", method = RequestMethod.GET)
	@ResponseBody
	public RestResult getInnovateItem(Integer id) {
		if(StringUtils.isEmpty(id)) {
			return new RestResult(false, "请输入创意作品Id");
		}
		Optional<Innovate> inn = this.innovateDao.findById(id);
		if(!inn.isPresent()) {
			return new RestResult(false, "创意作品不存在");
		}
		Innovate innovate = inn.get();
		InnovateVO vo = new InnovateVO();
		vo.setDescription(innovate.getDescription());
		vo.setId(innovate.getId());
		vo.setMaxScore(innovate.getMaxScore());
		vo.setMinScore(innovate.getMinScore());
		vo.setScore(innovate.getScore());
		vo.setStatu(innovate.getStatu());
		vo.setTitle(innovate.getTitle());
		vo.setOnscore(innovate.getOnscore());
		vo.setAttscore(innovate.getAttscore());
		return new RestResult(vo);
	}
	
	@RequestMapping(value = "/submitGrade", method = RequestMethod.POST)
	@ResponseBody
	public RestResult submitGrade(@RequestBody GradeVO vo) throws GradeException {
		User user = (User)SecurityUtils.getSubject().getPrincipal();
		GradeRecord rec = new GradeRecord();
		rec.setInnovateId(vo.getInnovateId());
		rec.setScore1(vo.getScore1());
		rec.setScore2(vo.getScore2());
		rec.setScore3(vo.getScore3());
		rec.setScore4(vo.getScore4());
		rec.setScore5(vo.getScore5());
		rec.setScore6(vo.getScore6());
		rec.setScore7(vo.getScore7());
		rec.setUsername(user.getUsername());
		this.gradeService.submitGrade(rec);
		return new RestResult();
	}
	
	@RequestMapping(value = "/getInnovateResult", method = RequestMethod.GET)
	@ResponseBody
	public RestResult getInnovateResult(){
		List<Innovate> list = this.innovateDao.findByOrderByScoreDesc();
		List<InnovateVO> result = new ArrayList<InnovateVO>();
		for(Innovate i: list) {
			if(i.getStatu() != 2) {
				continue;
			}
			InnovateVO vo = new InnovateVO();
			vo.setDescription(i.getDescription());
			vo.setId(i.getId());
			vo.setMaxScore(i.getMaxScore());
			vo.setMinScore(i.getMinScore());
			vo.setScore(i.getScore());
			vo.setStatu(i.getStatu());
			vo.setTitle(i.getTitle());
			result.add(vo);
		}
		return new RestResult(result);
	}
	
	@RequestMapping(value = "/manage", method = RequestMethod.GET)
    public String manage() {
        return "manage";
    }
	
	@RequestMapping(value = "/enableGrade", method = RequestMethod.POST)
	@ResponseBody
	public RestResult enableGrade(Integer id) throws GradeException {
		this.gradeService.enableGrade(id);
		return new RestResult();
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public RestResult list(){
		List<Innovate> list = this.innovateDao.findAll();
		list.sort((i1, i2) -> i1.getDisplayOrder().compareTo(i2.getDisplayOrder()));
		List<InnovateVO> result = new ArrayList<InnovateVO>();
		for(Innovate i: list) {
			InnovateVO vo = new InnovateVO();
			vo.setDescription(i.getDescription());
			vo.setId(i.getId());
			vo.setMaxScore(i.getMaxScore());
			vo.setMinScore(i.getMinScore());
			vo.setScore(i.getScore());
			vo.setStatu(i.getStatu());
			vo.setTitle(i.getTitle());
			vo.setStatu(i.getStatu());
			result.add(vo);
		}
		return new RestResult(result);
	}
}
